//
//  DFStatusLIstViewModel.swift
//  新浪微博
//
//  Created by dafei on 16/5/25.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class DFStatusLIstViewModel: NSObject {
    
    //定义模型数组
    lazy var statuesViewModelArray: [DFStatuesViewModel] = [DFStatuesViewModel]()
    
    
    //闭包执行GCD的回调   在oc 中用Block  
     func loadData(finished: (isSuccess: Bool) -> () ) {
        HMNetworkTools.sharedTools.loadHomePageData { (responseObject, error) in
            if error != nil {
                //执行回调
                finished(isSuccess: false)
                return
            }
//            print(responseObject)
            //处理数据 AFN 已经将数据处理成了 0c之后数组或字典
            guard let dict = responseObject as? [String: AnyObject] else {
                //执行失败的回调
                finished(isSuccess: false)
                return
            }
            //            print(dict)
            //根据 'statuses' 获取数组
            guard let array = dict["statuses"] as? [[String: AnyObject]] else {
                //执行失败的回调
                finished(isSuccess: false)
                return
            }
            //            print(array)
            //遍历数组 获取字典
            //实例化空数组
            var tempArry = [DFStatuesViewModel]()
            for item in array {
                
                //字典转模型
                let s = DFStatus(dict: item)
                //在数组中添加视图模型
                let viewmedol = DFStatuesViewModel(statues: s)
                //添加到组数中
                tempArry.append(viewmedol)
            }
            //赋值保存
            self.statuesViewModelArray = tempArry
           
            //执行回调
            finished(isSuccess: true)
            
        }
        
    }
    

}
